package com.scalar.chiptrack.operations.dataentry.transit.actioncommands;

import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.transit.forms.TransitDataEntryForm;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.process.dao.PartNumber;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.utils.ConfigManager;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Sep 26, 2006
 * Time: 2:34:28 AM
 */
public class TransitDataEntryLoadLotNumbersCommand extends ActionCommand
{

    private static final String ACTION_NAME = "/transitDataEntry.do" ;


    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {
        TransitDataEntryForm transitDataEntryForm = ( TransitDataEntryForm ) form;

        HashMap<String,String> errors = new HashMap<String,String>();
        HashMap<String,String> info   = new HashMap<String,String>();
        String action = request.getParameter("action");

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }

        ArrayList<LabelValueBean> transitPartNumbersList    = new ArrayList<LabelValueBean>();
        ArrayList<LabelValueBean> transitfabLotList         = new ArrayList<LabelValueBean>();
        ArrayList<LabelValueBean> transitLotList            = new ArrayList<LabelValueBean>();

        try
        {
            ArrayList<PartNumber> partNumberList = DataEntryDAO.getPartNumberByPsType( transitDataEntryForm.getProcessType() );
            for (int i = 0; i < partNumberList.size(); i++)
            {
                PartNumber partNumber = (PartNumber) partNumberList.get(i);
                transitPartNumbersList.add(new LabelValueBean(partNumber.getPartNumber()/*+"/"+ partNumber.getProcessType()*/, partNumber.getProcessStepId()));
            }

            ArrayList fabLotNumberList = DataEntryDAO.getTransitFabLots( Integer.parseInt( transitDataEntryForm.getProcessStepId() ), Integer.parseInt( transitDataEntryForm.getLocationId() ) );
            for (int i = 0; i < fabLotNumberList.size(); i++)
            {
                LotDetails lotDetails = (LotDetails) fabLotNumberList.get(i);
                transitfabLotList.add(new org.apache.struts.util.LabelValueBean( lotDetails.getFabLotNumber(), lotDetails.getFabLotNumber()));
            }

            ArrayList lotList = DataEntryDAO.getTransitLots( transitDataEntryForm.getProcessStepId(), transitDataEntryForm.getFabLotNo() );
            for (int i = 0; i < lotList.size(); i++)
            {
                LotDetails lotDetails = (LotDetails) lotList.get(i);
                if (ConfigManager.getBooleanProperty(ConfigManager.SHOW_SHIPPED_LOT_BY_WAFER_ID ) )
                {
                   transitLotList.add(new org.apache.struts.util.LabelValueBean( lotDetails.getLotNumber()+"#" + lotDetails.getWaferIds(), lotDetails.getShippedLotId())); 
                }
                else
                {
                    transitLotList.add(new org.apache.struts.util.LabelValueBean( lotDetails.getLotNumber(), lotDetails.getShippedLotId()));
                }
            }
            transitDataEntryForm.setPoId("0");
            request.setAttribute("processTypeList", NewProductDAO.loadAllProcessesFromDB(null, false));
            request.setAttribute("partNumbersList", transitPartNumbersList);
            request.setAttribute("locationList", DataEntryDAO.loadAllVendorsByPsType( null, transitDataEntryForm.getProcessType() ) );
            request.setAttribute("transitFabLotNoList", transitfabLotList);
            request.setAttribute("transitList", transitLotList);
            request.setAttribute("action", action);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return mapping.findForward("transitDataEntry");
    }
}